package com.zijiebutiao.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.SyncReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zijiebutiao.pojos.TOrdersetting;
import com.zijiebutiao.service.TOrdersettingService;
import com.zijiebutiao.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/2/28 20:47
 * @Author 时间刺客
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/ordersetting")
@Api(tags = "预约设置")
public class TOrdersettingController {

    @Autowired
    private TOrdersettingService tOrdersettingService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传excel",notes = "以二进制文件上传",response = boolean.class)
    public ResultVo uploadFile(MultipartFile file) throws IOException {

        ResultVo resultVo=new ResultVo();
        if (file==null){
            resultVo.setData(false);
            resultVo.setCode(500);
            resultVo.setErrorMsg("文件不能为空");
            return resultVo;
        }
            //原始文件名
            String originalFilename = file.getOriginalFilename();//abc.jpg
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

            List<TOrdersetting> list=new ArrayList<>();

                //判断是否为exel文件
                if (".xls".equals(suffix)||".xlsx".equals(suffix)) {
                    //是附件
                    InputStream inputStream = file.getInputStream();
                    EasyExcel.read(inputStream, TOrdersetting.class, new SyncReadListener() {
                        @Override
                        public void doAfterAllAnalysed(AnalysisContext arg0) {
                            System.out.println("Excel全部读完被执行......");
//                            tOrdersettingService.addOrUpdate(list);
                        }
                        @Override
                        public void invoke(Object data, AnalysisContext arg1) {
                            //读完一行就执行一次(调用数据库进行插入操作)
                            System.out.println("解析一行: " + data);
                            list.add((TOrdersetting) data);
                        }
                    }).doReadAll();
                } else {
                    //普通表单
                    resultVo.setData(false);
                    resultVo.setCode(500);
                    resultVo.setErrorMsg("文件格式错误!");
                    return resultVo;
                }
            tOrdersettingService.addOrUpdate(list);
            //上传完成进行转发或者重定向
            resultVo.setData(true);
            return resultVo;
    }
    @ApiOperation(value = "获取本月数据",notes = "",response = List.class)
    @GetMapping("/getOrderSetting/{yearmonth}")
    public ResultVo getOrderSettinglist(@PathVariable(name = "yearmonth") String yearmonth){
        ResultVo resultVo=new ResultVo();
        String beginDay=yearmonth+"-"+1;
        String endDay=yearmonth+"-"+31;

        resultVo.setData(tOrdersettingService.findByMonth(beginDay,endDay));
        return  resultVo;
    }
    @ApiOperation(value = "设置可预约数",notes = "",response = boolean.class)
    @PostMapping("/editNumberByOrderDate")
    public  ResultVo editNumberByOrderDate(@RequestBody TOrdersetting tOrdersetting){
        LambdaQueryWrapper<TOrdersetting> qw= Wrappers.lambdaQuery();
        qw.eq(TOrdersetting::getOrderdate,tOrdersetting.getOrderdate());
        TOrdersetting ordersetting = tOrdersettingService.getOne(qw);
        if (ordersetting==null){
            return ResultVo.success(tOrdersettingService.save(tOrdersetting));
        }
        if (tOrdersetting.getNumber()>=ordersetting.getReservations()){
            return ResultVo.success(tOrdersettingService.update(tOrdersetting,qw));
        }

        return ResultVo.success(false);
    }
}
